<?php
  $department = [
    ['id'=> 1,'name'=> 'make','age'=> 21,'title'=> 'lealer','salary'=>30000],
    ['id'=> 2,'name'=> 'mary','age'=> 43,'title'=> 'developer','salary'=>10000],
    ['id'=> 3,'name'=> 'john','age'=> 23,'title'=> 'text3','salary'=>5000],
    ['id'=> 4,'name'=> 'kclo','age'=> 34,'title'=> 'text4','salary'=>8000],
    ['id'=> 5,'name'=> 'chen','age'=> 30,'title'=> 'text5','salary'=>15000]
];

if (isset($_GET['a'])&& $_GET['a'] == 'age') {
	//按年龄排序员工数据
	usort($department, 'compareByAge');
}
  function compareByAge($emp1,$emp2){
  	 return ($emp1['age'] - $emp2['age']);
  }


if (isset($_GET['s'])&& $_GET['s'] == 'salary') {
	
	usort($department,'compareBySalary');
}
  function compareBySalary($emp1,$emp2){
  	 return ($emp1['salary'] - $emp2['salary']);
  }


  if (isset($_GET['id'])&& $_GET['id'] == 'id') {
	
	usort($department,'compareById');
}
  function compareById($emp1,$emp2){
  	 return ($emp1['id'] - $emp2['id']);
  }
  
 

 //表单提交后，当前需要进行的请求方式是POST，响应表单提交
 if (($_SERVER['REQUEST_METHOD']) === 'POST') {
 	//获取用户提交上来的数据，http协议当中，POST用户输出的数据是作为请求正文请求实体，提交的。在PHP当中通过POST来请求    htmlspecialchars  过滤字符串中的html中的特殊字符
 	                //htmlentities  函数html实体字符
 	                //用户数据必须过滤
	$searchKey = isset($_POST['search_key'])? trim(htmlspecialchars($_POST['search_key'])):'';
   //便利数组，按照title来查找，忽略大小写，使用if判断，输入按title前缀匹配的参数定义，需要查询的子字符串，建立新的字符串。
   $searchDepar = [];
   foreach ($department as $emp) {  
   if (stripos($emp['title'],$searchKey) === 0) {
    //若匹配，则把整个员工信息输出。
    $searchDepar[] = $emp;
   } else if ($searchKey === '') {
   	$searchDepar[] = $emp;
   }
  }
  $department = $searchDepar;
}


  if(($_SERVER['REQUEST_METHOD']) === 'POST') {
  	$searKey = isset($_POST['sear_key'])? ($_POST['sear_key']):'';
  	 
  }


?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
	<title>表格数组</title>
	    <style type="text/css">
       body {
         margin: 0;
         font-family: "Microsoft YaHei", "微软雅黑", "Helvetica Neue", Helvetica, Arial, sans-serif;
         font-size: 14px;
         color: #777; 
       	}

       	#wrapper{
       	 width: 90%;
       	 min-height: 290px;
       	 margin: 0 auto;
       	 padding-top: 10px;
       	}

       	#wrapper table,#wrapper table th,#wrapper table td{
         border: 1px solid #ccc;
         border-collapse: collapse;
         border-spacing: 0;
        }

        #wrapper table{
         width: 100%;
           margin: 0 auto;
        }

        #wrapper table tr{
          height: 40px;         
        }

        #wrapper table th{
          font-size: 16px;
          font-weight: bolder;
          color: #222;
          background: #f7f7f7;
        }

        #wrapper table td{
          text-align: center;
        }

        #wrapper table .data-row:hover{
           background: #e9e9e9;
        }
        .btn a:hover{
        	color: #e9e9e9;
        }
        .btn{
        	float: left;
        	display: block;
        	width: 70px;
        	height: 40px;
        	line-height: 40px;
        	background: #777;
        	text-align: center;
        	text-decoration: none;
        	margin: 5px;
        	border-radius: 5px;
        }


      </style>
</head>
<body>
   <div id="wrapper">
     <form method="post">
     <input type="text" name="search_key" placeholder="title"/>
     <input type="submit" value="查找">
     </form>
    <div>
     <form method="post">
      <select>

        <option value="0">id</option>
        <option value="1">age</option>
        <option value="2">salary</option>
        <input type="submit" value="确定">
      </select>
     </form>
    </div>
     <table>
       <tr>
         <th>id</th>
         <th>name</th>
         <th>age</th>
         <th>title</th>
         <th>salary</th>
         <th>actions</th>
       </tr>
       <?php foreach ($department as $emp): ?>
       <tr class="data-row">
         <td><?= $emp['id'] ?></td>
         <td><?= $emp['name'] ?></td>
         <td><?= $emp['age'] ?></td>
         <td><?= $emp['title'] ?></td>
         <td><?= $emp['salary'] ?></td>
         <td><a href="data_datail.php?id=<?= $emp['id']?>&n=<?= $emp['name']?>&a=<?= $emp['age']?>&t=<?= $emp['title']?>&s=<?= $emp['salary']?>">datail</a></td>
       </tr>
       <?php endforeach; ?>
     </table>
    </div>

</body>
</html>